package com.esfak47.mediafetcher.app.event.eventhandler;

import com.esfak47.common.lang.Inject;
import com.esfak47.mediafetcher.core.constants.Constants;
import com.esfak47.mediafetcher.core.domain.Media;
import com.esfak47.mediafetcher.core.event.Event;
import com.esfak47.mediafetcher.core.event.EventGroupEnum;
import com.esfak47.mediafetcher.core.service.MediaStorage;
import com.google.common.collect.Sets;

/**
 * @author tony
 * @date 2018/5/21
 */
public class MediaFetchedEventHandler extends AbstractEventHandler {

    @Inject
    private MediaStorage mediaStorage;

    public MediaFetchedEventHandler() {
        super(Sets.newHashSet(Constants.MediaSubjects.fetched.name())
            , Sets.immutableEnumSet(EventGroupEnum.MEDIA));
    }

    @Override
    public void onEvent(Event event) {
        if (event.getContent() != null && (event.getContent() instanceof Media)) {
            final Media content = (Media)event.getContent();
            mediaStorage.save(content);
        }
    }
}
